

#include <gtk/gtk.h>
#include "support.h"
#include "window.h"
#include "menubar.h"
#include "config.h"
#include "toolbar.h"
#include "toolbar_second.h"
#include "codeview.h"
#include "regflag.h"
#include "memview.h"
#include "ioview.h"
#include "sourceview.h"
#include "statusbar.h"
#include "keypad.h"
#include "gsim.h"

/*#### define globles ####*/
GtkWidget *gsim_appwindow;
GtkTooltips *gsim_tooltips;
GtkWidget *gsim_regflag_table;
GtkWidget *gsim_memview_box;
GtkWidget *gsim_ioview_box;
GtkWidget *gsim_sourceview_box;
GtkWidget *gsim_sourceview;
GtkWidget *gsim_keypad;
gint state_memview=1;
gint state_ioview=1;
gint state_sourceview=1;
gint state_regflag=1;
gint state_keypad;
GdkPixbuf *code_tracker_pixmap,*data_tracker_pixmap,*code_brpoint_pixmap;

/*################# satitic functions ###################*/
static gboolean del_event_handler(GtkWidget *window,GdkEvent *event,gpointer data);
static void register_stock_items (void);
static GtkWidget *gsim_init_gui( void );

/*################## Register the custom stock items ################*/
void register_stock_items (void)
{
	GtkIconFactory *factory;
	gint i ;

 	static GtkStockItem gsim_stock_items[ ]  = {
		{ "keypad","_Keypad", 0, 0, NULL },
		{ "register","_Register", 0, 0, NULL },
		{ "memory","_Memory", 0, 0, NULL },
		{ "port","_Port", 0, 0, NULL },
		{ "source","_Source", 0, 0, NULL },
		{ "about","_About", 0, 0, NULL },
		{ "run","_RUN ", 0, 0, NULL },
		{ "step","_STEP", 0, 0, NULL },
		{ "small-close",NULL, 0, 0, NULL },
		{ "find",NULL, 0, 0, NULL }
	};
     
	gchar *gsim_icon_files[ ] = {
		"keypad.png",
		 "reg.png",
		 "memory.xpm",
		 "port.xpm",
		 "source.png",
		 "about.png",
		 "run.png",
		 "step.png",
		 "small-close.xpm",
		 "find.png"
	 };
	 
	/* Register our stock items */ 
	gtk_stock_add(gsim_stock_items, G_N_ELEMENTS (gsim_stock_items));
      
	/* Add our custom icon factory to the list of defaults */
	factory = gtk_icon_factory_new ( );
	gtk_icon_factory_add_default (factory);

 	/* Register icon to accompany stock item */
	for( i=0 ; i<10 ;i++ )
	{
		GtkIconSet *icon_set;
		GdkPixbuf *pixbuf;
		//GString *filename;
		gchar *filename;
		 
		//filename=g_string_new( PACKAGE_PIXMAPS_DIR"/" );
        //g_string_append( filename , gsim_icon_files[ i ] );
#ifdef G_OS_WIN32
		filename=g_build_filename (package_pixmaps_dir, gsim_icon_files[ i ], NULL);
#else
        filename=g_build_filename (PACKAGE_PIXMAPS_DIR, gsim_icon_files[ i ], NULL);
#endif
		pixbuf = gdk_pixbuf_new_from_file( filename/*->str*/, NULL );       
		if( pixbuf )
		{
			icon_set = gtk_icon_set_new_from_pixbuf (pixbuf);
			gtk_icon_factory_add (factory, gsim_stock_items[ i ].stock_id , icon_set);
			//g_string_free( filename , TRUE );
			g_free(filename);
			gtk_icon_set_unref (icon_set);
			g_object_unref (pixbuf);
		}
		else
		{
			g_warning(_("Failed to load stock item %s\n"),filename/*->str*/ );
			//g_string_free( filename , TRUE );
			g_free(filename);
        }
	}
	g_object_unref( factory );
	
	return ;
}

	
/*######## Initilise and pack application's various widgets ########*/
static GtkWidget *gsim_init_gui( void )
{
	GtkWidget *box ;
	
	box=gtk_vbox_new( FALSE , 0);
	
	/*pack the menu bar*/
	GtkWidget *gsim_menubar;
	gsim_menubar=gsim_get_menubar( );
	gtk_box_pack_start( GTK_BOX(box) , gsim_menubar ,FALSE , FALSE , 0);
	
	/*box where tool bar & a image packed*/
	GtkWidget *toolbar_n_image_box;
	toolbar_n_image_box=gtk_hbox_new(FALSE , 0);        
	gtk_box_pack_start( GTK_BOX(box) , toolbar_n_image_box ,FALSE , FALSE , 0);
	
	/*pack the toolbar*/
	GtkWidget *gsim_toolbar;
	gsim_toolbar=gsim_get_toolbar( );
	gtk_box_pack_start( GTK_BOX(toolbar_n_image_box) , gsim_toolbar ,TRUE , TRUE , 0);
	
	/*pack the gsim image right to toolbar*/
	
	
	/*box where codetree memory & port list packed */
	GtkWidget *treeviews_box;
	treeviews_box=gtk_hbox_new( FALSE , 0 );
	gtk_box_pack_start( GTK_BOX(box) , treeviews_box ,TRUE , TRUE , 0);
	
	/*box where codetree registers and flags  packed */
	GtkWidget *code_n_registers_box;
	code_n_registers_box=gtk_vbox_new( FALSE , 0 );
	gtk_box_pack_start( GTK_BOX(treeviews_box) ,code_n_registers_box,TRUE , TRUE , 0);
	
	/*pack the start address entry and run-stop button*/
	GtkWidget *gsim_toolbar_second;
	gsim_toolbar_second=gsim_get_toolbar_second( );
	gtk_box_pack_start( GTK_BOX(code_n_registers_box),gsim_toolbar_second,FALSE , FALSE , 0);
	
	/*pack the status bar*/
	GtkWidget *status_box;
	status_box=gsim_get_status_box( );
	gtk_box_pack_end(GTK_BOX(box) , status_box ,FALSE , FALSE , 0);
	
	/*pack the codetree box*/
	GtkWidget *gsim_codeview;
	gsim_codeview=gsim_get_codview();
	gtk_box_pack_start( GTK_BOX(code_n_registers_box),gsim_codeview,TRUE , TRUE , 0);
	
	/*pack the register and flag box */
	gsim_regflag_table = gsim_get_regflag_table( );
	gtk_box_pack_start( GTK_BOX(code_n_registers_box),gsim_regflag_table,FALSE , FALSE , 0);
	
	/*pack the memory view */
	gsim_memview_box=gsim_get_memview_box( );
	gtk_box_pack_start( GTK_BOX(treeviews_box) ,gsim_memview_box,FALSE , FALSE , 0);
	
	/*pack the I/O view */
	gsim_ioview_box=gsim_get_ioview_box( );
	gtk_box_pack_start( GTK_BOX(treeviews_box) ,gsim_ioview_box,FALSE , FALSE , 0);
	
	/*pack the dockable source view */
	gsim_sourceview_box=gtk_vbox_new(FALSE,0);
	gsim_sourceview=gsim_get_sourceview_box( );
	gtk_box_pack_start(GTK_BOX(gsim_sourceview_box),gsim_sourceview,FALSE,FALSE,0);
	gtk_box_pack_start( GTK_BOX(box) , gsim_sourceview_box ,FALSE , FALSE , 0);
	
	return box;
}


/*############ Main window's delete event handler##############*/
static gboolean del_event_handler(GtkWidget *window,GdkEvent *event,gpointer data)
{
	return FALSE;
}

/*############### Quit from the application ##################*/
void quit_from_main(GtkWidget *window,gpointer usr_data)
{
	make_config_file();
	gtk_main_quit();
}


/*################ Create the main window ##################*/
void  gsim_create_appwindow ( void)
{
	gsim_appwindow=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	
	g_object_set( gsim_appwindow ,"window-position",GTK_WIN_POS_CENTER,
																"default-height",550,
																"default-width",750,
																"title","GSim85",NULL);
						
        gchar *filename_tl,*filename_ct,*filename_dt,*filename_cb;										
#ifdef G_OS_WIN32
		filename_tl=g_build_filename (package_pixmaps_dir, "gsim.png", NULL);
		filename_ct=g_build_filename (package_pixmaps_dir, "ctracker.xpm", NULL);
		filename_dt=g_build_filename (package_pixmaps_dir, "dtracker.xpm", NULL);
		filename_cb=g_build_filename (package_pixmaps_dir, "cbrpoint.xpm", NULL);
#else
        filename_tl=g_build_filename (PACKAGE_PIXMAPS_DIR, "gsim.png", NULL);
        filename_ct=g_build_filename (PACKAGE_PIXMAPS_DIR, "ctracker.xpm", NULL);
        filename_dt=g_build_filename (PACKAGE_PIXMAPS_DIR, "dtracker.xpm", NULL);
        filename_cb=g_build_filename (PACKAGE_PIXMAPS_DIR, "cbrpoint.xpm", NULL);
#endif
	//gtk_window_set_default_icon_from_file(PACKAGE_PIXMAPS_DIR"/gsim.png",NULL);
	gtk_window_set_default_icon_from_file(filename_tl,NULL);
	
	
	/*enable tooltips*/
	gsim_tooltips=gtk_tooltips_new( );
	
	/* initilise GUI */
	GtkWidget *gsim_appbox;
	register_stock_items( );
	
	/*load small tracker icons*/
	code_tracker_pixmap=gdk_pixbuf_new_from_file(filename_ct,NULL);
	data_tracker_pixmap=gdk_pixbuf_new_from_file(filename_dt,NULL);
	code_brpoint_pixmap=gdk_pixbuf_new_from_file(filename_cb,NULL);
	
	g_free(filename_tl);
    g_free(filename_ct);
    g_free(filename_dt);
    g_free(filename_cb);
    	
	gsim_appbox=gsim_init_gui( );
	gtk_container_add( GTK_CONTAINER( gsim_appwindow ) , gsim_appbox );
	gtk_widget_show_all( gsim_appwindow );
	
	show_hide_sourceview(); /*hide the sourceview*/
	
	/*make the keypad*/
	gsim_keypad=gsim_create_keypad();

	/* callbacks of main window & keypad(when x button on title bar is clicked) */
	g_signal_connect(G_OBJECT(gsim_appwindow),"delete-event",
						G_CALLBACK(del_event_handler),NULL);
	g_signal_connect(G_OBJECT(gsim_appwindow),"destroy",
						G_CALLBACK(quit_from_main),NULL);
		
	gsim_update_statusbar(DESC_SBAR,_("Application launched successfully"));
	
	return ;

}
